package com.controller.backend;

import com.biz.ManagerInfoBiz;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.cond.BaseCond;
import com.entity.model.ManagerInfo;
import com.github.pagehelper.PageHelper;
import com.util.SystemConstant;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by asan on 2017/09/17.
 */
@Controller
@RequestMapping("/backend/security/user")
public class ManagerInfoController extends BaseController<ManagerInfo> {
    @Autowired
    private ManagerInfoBiz managerInfoBiz;

    //员工列表
    @RequestMapping(value = "index")
    public String index() throws Exception {
        return "/backend/security/user/list";
    }

    //员工详细信息
    @RequestMapping(value = "detail")
    public String detail() throws Exception {
        return "/backend/security/user/form";
    }

    //员工权限设置
    @RequestMapping(value = "authSet")
    public String authSet() throws Exception {
        return "/backend/security/user/authForm";
    }

    //修改密码
    @RequestMapping(value = "changePswInfo")
    public String changePswInfo() throws Exception {
        return "/backend/security/user/changePwd";
    }


    /*    @RequestMapping(value = "queryInfo")
        @ResponseBody
        public Result queryInfo(@RequestParam Integer id) {
            return ResultGenerator.genSuccessResult(menuInfoBiz.queryInfo(id));
        }*/
    //新增员工
    @RequestMapping(value = "addUser", method = RequestMethod.POST)
    @ResponseBody
    public Result addUser(ManagerInfo managerInfo) {
        if (managerInfo != null) {
            Condition cond = new Condition(ManagerInfo.class);
            cond.createCriteria().andEqualTo("account", managerInfo.getAccount());
            if (managerInfoBiz.countByCond(cond) > 0) {
                return ResultGenerator.genFailResult("该账号已存在");
            } else {
                managerInfo.setPassword(DigestUtils.md5Hex(managerInfo.getAccount() + managerInfo.getPassword()));
                managerInfoBiz.save(managerInfo);
                return ResultGenerator.genSuccessResult(managerInfo);
            }
        } else {
            return ResultGenerator.genFailResult("操作失败");
        }
    }

    //修改员工信息(只允许修改密码)
    @RequestMapping(value = "updateUser")
    @ResponseBody
    public Result updateUser(ManagerInfo managerInfo) {
        if (managerInfo != null) {
            Condition cond = new Condition(ManagerInfo.class);
            cond.createCriteria().andEqualTo("account", managerInfo.getAccount());
            if (managerInfoBiz.findByCondition(cond).size() > 0) {
                if (managerInfoBiz.findByCondition(cond).get(0).getId().equals(managerInfo.getId())) {
                    if (managerInfo.getPassword() != "") {
                        managerInfo.setPassword(DigestUtils.md5Hex(managerInfo.getAccount() + managerInfo.getPassword()));
                    }
                    managerInfoBiz.update(managerInfo);
                    return ResultGenerator.genSuccessResult(managerInfo);
                } else {
                    return ResultGenerator.genFailResult("该账号已存在");
                }
            } else {
                if (managerInfo.getPassword() != "") {
                    managerInfo.setPassword(DigestUtils.md5Hex(managerInfo.getAccount() + managerInfo.getPassword()));
                }
                managerInfoBiz.update(managerInfo);
                return ResultGenerator.genSuccessResult(managerInfo);
            }
        } else {
            return ResultGenerator.genFailResult("操作失败");
        }
    }

    //重写list,根据不同条件查询用
    @Override
    public Result list(BaseCond baseCond, ManagerInfo managerInfo) {
        PageHelper.startPage(baseCond.getPageNum(), baseCond.getPageSize(), false);
        PageHelper.orderBy(baseCond.getOrderBy());
        Condition cond = new Condition(ManagerInfo.class);
        Example.Criteria criteria = cond.createCriteria();
        if (StringUtils.isNoneBlank(managerInfo.getAccount())) {
            criteria.andLike("account", "%" + managerInfo.getAccount() + "%");
        }
        if (StringUtils.isNoneBlank(managerInfo.getUserName())) {
            criteria.andLike("userName", "%" + managerInfo.getUserName() + "%");
        }
        criteria.andGreaterThan("id", 1);
        List<ManagerInfo> list = baseBiz.findByCondition(cond);
        Integer total = baseBiz.countByCond(cond);
        ;
        return ResultGenerator.genSuccessResult(list, total);
    }

    //修改密码

    @RequestMapping(value = "changePwd", method = RequestMethod.POST)
    @ResponseBody
    public Result changePwd(String pwd, String stdPwd, HttpSession session) {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);
        String str = DigestUtils.md5Hex(managerInfo.getAccount() + stdPwd);
        if (str.equals(managerInfo.getPassword())) {
            managerInfo.setPassword(DigestUtils.md5Hex(managerInfo.getAccount() + pwd));
            managerInfoBiz.update(managerInfo);
            return ResultGenerator.genSuccessResult();
        }
        return ResultGenerator.genFailResult("原密码错误,无法修改");
    }

    @RequestMapping(value = "getAdmin", method = RequestMethod.POST)
    @ResponseBody
    public Result getAdmin(HttpSession session) {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);
        return ResultGenerator.genSuccessResult(managerInfo);
    }

}
